package com.javabi.common.date;

import java.util.concurrent.TimeUnit;

/**
 * The UnitTime class represents both a {@link TimeUnit} and a simple value associated with it.
 * <p>
 * Almost everywhere {@link TimeUnit} is used, a value for the time unit is required. So here we provide it in a single
 * class.
 * </p>
 */
public class UnitTime {

	private final long time;
	private final TimeUnit unit;

	public UnitTime(long time, TimeUnit unit) {
		if (time < 0) {
			throw new IllegalArgumentException("time=" + time);
		}
		if (unit == null) {
			throw new NullPointerException("unit");
		}
		this.time = time;
		this.unit = unit;
	}

	public long getTime() {
		return time;
	}

	public long getTime(TimeUnit unit) {
		return unit.convert(getTime(), getUnit());
	}

	public TimeUnit getUnit() {
		return unit;
	}

	@Override
	public String toString() {
		return time + "x" + unit;
	}

}
